<?php
Zend_Loader::loadClass("Application_Ajax_Server_Function");
Zend_Loader::loadClass("GlazingPanesGases");
class GetGlazingDetailsById extends Application_Ajax_Server_Function
{
	public function call() {
		$id = (int) $this->_params['id'];
		Zend_Loader::loadClass('Glazing');
		$vetricomposti = new Glazing();

		$cm = new GlazingPanesGases();
		$recordSet = $vetricomposti->find($id);
		$vetrocomposto = $recordSet->current();
		$props = $vetrocomposto->findDependentRowset("GlazingProps");
		$glazing_panes_gases = $vetrocomposto->findDependentRowset("GlazingPanesGases");
		$panes = $vetrocomposto->findManyToManyRowset("Panes", "GlazingPanesGases");
		$gases = $vetrocomposto->findManyToManyRowset("Gases", "GlazingPanesGases");

		$result = array(
				$vetrocomposto->toArray(),
				'category' => $vetrocomposto->findParentRow('GlazingCategories')->name,
				'props' => $props->toArray(),
				'panes' => array(),	// Riempito sotto
				'gases' => array(),	// Riempito sotto
				'prev' => 0,		// Riempito sotto
				'next' => 0			// Riempito sotto
		);

		/////////////QUELLO CHE SEGUE è DA RIVEDERE E CAMBIARE//////////////////////////
		$layers = 0;
		foreach ($panes as $k => $m) {
			if ($glazing_panes_gases[$k]->thickness != 0) {
				$layers++;
				$result['panes'][$k] = $m->toArray();
				$result['panes'][$k]['layer'] = $glazing_panes_gases[$k]->layer;
				$result['panes'][$k]['thickness'] = $glazing_panes_gases[$k]->thickness;
				$result['panes'][$k]['props'][0] = $m->findDependentRowset("GlazingPanesGases")->toArray();
				
			}
		}
		
		foreach ($gases as $k => $m) {
			if ($glazing_panes_gases[$k]->thickness != 0) {
				$layers++;
				$result['gases'][$k] = $m->toArray();
				$result['gases'][$k]['layer'] = $glazing_panes_gases[$k]->layer;
				$result['gases'][$k]['thickness'] = $glazing_panes_gases[$k]->thickness;
				$result['gases'][$k]['props'][0] = $m->findDependentRowset("GlazingPanesGases")->toArray();
		
			}
		}
		
		$result['layers'] = $layers;
		function cmp($a, $b) {
			if ($a['layer'] == $b['layer']) return 0;
			return ($a['layer'] > $b['layer']) ? 1 : -1;
		}
		usort ($result['panes'], "cmp");

		// Build prev/next circular array
		if (!$vetrocomposto->id_category) {
			$recordSet = $vetricomposti->select()->from($vetricomposti, array("id"))
			->where("id_category is null")
			->where("status > ?", 0)
			->order("name");
		} else {
			$recordSet = $vetricomposti->select()->from($vetricomposti, array("id"))
			->where("id_category = ?", $vetrocomposto->id_category)
			->where("status > ?", 0)
			->order("name");
		}

		$ids = $vetricomposti->fetchAll($recordSet)->toArray();
		foreach ($ids as $k => $v)
		{
			if($v["id"] == $id)
			{
				if (isset($ids[$k-1]))
					$prev = $ids[$k-1];
				else
					$prev = end($ids);
				if (isset($ids[$k+1]))
					$next = $ids[$k+1];
				else
					$next = $ids[0];
			}
		}
		$result['prev'] = $prev["id"];
		$result['next'] = $next["id"];

		return $result;
	}
}